/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package security;


import helpers.Debug;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;


/**
 *
 * @author Bari
 */
public class Encryptor {

    private PrivateKey privateKey;
    private PublicKey publicKey;


    public Encryptor() throws Exception {


        publicKey = KeyKeeper.GetPublicKey();
        privateKey = KeyKeeper.GetPrivateKey();
       // Debug.logMessage("Encryptor", "Public: " + publicKey.getEncoded() + " Private:  " + privateKey.getEncoded());
      //  Debug.logMessage("Encryptor", "Public: " + publicKey.getFormat() + " Private:  " + privateKey.getFormat());
    }

    

    public String rsaEncrypt(String data) throws Exception {

        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] byteData = data.getBytes();
        byte[] encryptedData = cipher.doFinal(byteData);
       // String base64String=(byteData);
        return "";
    }

    public String rsaDecrypt(String data) throws Exception {
        byte[] receivedData=data.getBytes("UTF-8");
        byte[] byteData=Base64.decodeBase64(data);
       // Debug.logMessage("rsaDecrypt", "Bytedata: "+new String(byteData,"UTF-8"));
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        //byte[] byteData = data.getBytes();
        byte[] encryptedData = cipher.doFinal(byteData);
        return new String(encryptedData);
    }
}
